<div class="crumb-wrap">
    <div class="crumb-list"><i class="icon-font"></i><a href="<?=site_url('shops')?>">首页</a>
        <span class="crumb-step">&gt;</span><a class="crumb-name" href="<?=site_url('shops/goods')?>">商品管理</a></span>
        <span class="crumb-step">&gt;</span><span class="crumb-name">商品信息</span>
    </div>
</div>

<div class="search-wrap">
    <div class="search-content">
        <table class="search-tab" id="goods_menu">
            <tr>
                <td style="padding-left: 20px;"><input class="btn btn6" value="商品信息录入" type="button" ></td>
                <td style="padding-left: 20px;"><input class="btn btn6" value="商品详情" type="button"></td>
                <td style="padding-left: 20px;"><input class="btn btn7" value="商品属性（一）" type="button"></td>
                <td style="padding-left: 20px;"><input class="btn btn7 btn-primary" value="商品属性（二）" type="button"></td>
                <td style="padding-left: 20px;"><input class="btn btn6" value="商品评论管理" type="button"></td>
            </tr>
        </table>
    </div>
</div>
<div class="result-title" style="margin-left: 20px">
    <div class="result-list">
        <a href="javascript:void(0);" onclick="create()"><i class="icon-font"></i>新增属性类型</a>
        <a id="batchDel" href="javascript:void(0)" onclick="del_all()"><i class="icon-font"></i>批量删除</a>
        <a id="updateOrd" href="javascript:void(0)" onclick="location.reload();"><i class="icon-font"></i>更新排序</a>
    </div>
</div>

<div class="result-content">
<table class="result-tab" width="100%">
    <tr>
        <th class="tc" width="5%"><input class="allChoose" name="checkbox" id="all" type="checkbox" onclick="check_all()"></th>
        <th width="5%">编号</th>
        <th width="150">属性名称</th>
        <th>属性值</th>
        <th>排序</th>
        <th colspan ="2" width="50">操作</th>
    </tr>

    <?php $i = -1; //判断是否是一个大循环 ?>
    <?php foreach($res as $key => $value): ?>
        <?php foreach($value['tree'] as $k => $v):?>
        <tr>
            <td class="tc"><input name="checkbox" value="<?=$v['id']?>" type="checkbox"></td>
            <td><?=$v['id']?></td>

            <?php if($i != $value['id']):?>
            <td rowspan=<?=count($value['tree'])?>><?=$value['field']?></td>
            <?php endif;?>

            <td><?=$v['field']?></td>
            <td title="点击添加排序，值越大越靠前" onclick="update_sort(<?=$v['id']?>,<?=$v['sort']?>)" style="cursor:pointer;"><?=$v['sort']?></td>
            <td width="200px">
                <a class="link-update" href="javascript:void(0);" onclick="edit_pro(<?=$v['id']?>,'<?=$v['field']?>')">修改</a>
                <a class="link-del" href="javascript:void(0);" onclick="deletes(<?=$v['id']?>)">删除</a>
            </td>
            <?php if($i != $value['id']):?>
            <td rowspan=<?=count($value['tree'])?> width="220px">
                <a class="link-update" href="javascript:void(0);" onclick="create_val(<?=$value['id']?>)">添加属性值</a>
                &nbsp;&nbsp;
                <a class="link-update" href="javascript:void(0);" onclick="update_sort(<?=$value['id']?>,<?=$value['sort']?>)" >排序[<?=$value['sort']?>]</a>
            </td>
            <?php $i = $value['id']; ?>
            <?php endif;?>
        </tr>
        <?php endforeach;?>
    <?php endforeach;?>

</table>
<!-- <div class="list-page"> 2 条 1/1 页</div> -->
</div>

<script>
//加载菜单
$.get("<?=site_url('shops/widget/goods_menu?menu=4&gid='.$gid)?>",function(data){
    $("#goods_menu").html(data);
},'text');

layer.config({
    extend: 'extend/layer.ext.js'
});

function create(){
    layer.open({
        type: 2,
        title:'属性添加',
        area: ['700px', '330px'],
        fix: false, //不固定
        maxmin: true,
        content: "<?=site_url('shops/property/create?gid='.$gid)?>"
    });
}

/**
 * 修改属性
 */
function edit_pro(id,val){
    layer.prompt({
        formType: 2,
        value: val,
        maxlength: 140,
        title: '请输入属性值'
    }, function(value, index, elem){
        $.post("<?=site_url('shops/property/updateval')?>",{vid:id,val:value},function(data){
            layer.close(index);
            if(data == 1){
                layer.alert('操作成功！刷新页面即可查看信息',{
                    icon:1,
                },function(){
                    location.reload();
                });
            }else{
                layer.alert(data,{
                    icon:2,
                });
            }
        });

    });
}

/**
 * 添加属性值
 */
function create_val(vid){

    layer.prompt({
        formType: 2,
        value: '',
        maxlength: 140,
        title: '请输入属性值'
    }, function(value, index, elem){

        $.post("<?=site_url('shops/property/create_val')?>",{pid:vid,val:value},function(data){
            layer.close(index);
            if(data == 1){
                layer.alert('操作成功！刷新页面即可查看信息',{
                    icon:1,
                },function(){
                    location.reload();
                });
            }else{
                layer.alert(data,{
                    icon:2,
                });
            }
        });

    });
}

//分类选中
function check_all(){
    var v = $('#all').is(':checked');
    $("input[name='checkbox']:checkbox").each(function() {
        this.checked=v;
    })
}

/**
 * 点击排序
 */
function update_sort(id,sort){
    layer.prompt({
        formType: 0,
        value: sort,
        maxlength: 140,
        title: '请输入数值，大的靠前'
    }, function(value, index, elem){
        $.post("<?=site_url('shops/property/prosort')?>",{vid:id,sort:value},function(data){
            layer.close(index);
            if(data == 1){
                layer.alert('操作成功！刷新页面即可查看信息',{
                    icon:1,
                },function(){
                    location.reload();
                });
            }else{
                layer.alert(data,{
                    icon:2,
                });
            }
        });

    });
}

/**
 * 删除属性
 */
function deletes(vid){
    $.post("<?=site_url('shops/property/delete')?>",{pro_id:vid},function(data){
        if(data == 1){
            layer.alert('操作成功！刷新页面即可查看信息',{
                icon:1,
            },function(){
                location.reload();
            });
        }else{
            layer.alert(data,{
                icon:2,
            });
        }
    });
}

//批量删除
function del_all(){
    layer.confirm('删除之后不能恢复，需要继续吗？', {
        icon:0,
        btn: ['删除','取消'], //按钮
        shade: false //不显示遮罩
    }, function(){
        $("input[name='checkbox']").each(function() {
            if($(this).is(':checked')){
                var v = $(this).val();
                $.ajax({
                    url:"<?=site_url('shops/property/delete')?>",
                    type:"post",
                    async:false,
                    data:{pro_id:v}
                });
            }
        })
        location.reload(true);
    });
}
</script>